// 
// SoapTrace.Net - A soap message monitoring utility. 
// 
// Copyright (C) 2004 Matthew Ward
// 
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
// 
// Matthew Ward (mrward@users.sourceforge.net)
// 

using NUnit.Framework;
using System;
using System.IO;
using SoapTrace.Core.Communication;
using SoapTrace.Core.Formatters;
using SoapTrace.Tests.Config;
using SoapTrace.Tests.Utils;

namespace SoapTrace.Tests
{
	/// <summary>
	/// Test fixture for testing the formatting of compressed/base 64 text.
	/// </summary>
	[ TestFixture ]
	public class CompressedXmlSoapFormatterTestFixture
	{
		/// <summary>
		/// The soap message formatter configuration.
		/// </summary>
		private SoapMessageFormatterConfig m_Config = new SoapMessageFormatterConfig( );
		
		/// <summary>
		/// The soap message formatter under test.
		/// </summary>
		private SoapMessageFormatter m_Formatter;

		/// <summary>
		/// The name of the web service.  This is passed to the 
		/// soap message formatter object on formatting.
		/// </summary>

		private string m_WebServiceName = "Maths";

		/// <summary>
		/// The web service partial uri.  This is passed to the 
		/// soap message formatter object on formatting.
		/// </summary>
		private string m_Uri = "Maths.asmx";

		/// <summary>
		/// The name of the web service operation.  This is passed to the 
		/// soap message formatter object on formatting.
		/// </summary>
		private string m_WebServiceMethod = "Test";

		/// <summary>
		/// The mock soap message object that contains the 
		/// message to be formatted.
		/// </summary>
		private MockSoapMessage m_MockMessage;

		/// <summary>
		/// The soap response xml that contains a base 64 encoded string.
		/// </summary>
		private string m_ResponseXml = String.Empty;

		/// <summary>
		/// The expected response after it has been formatted.
		/// </summary>
		private string m_FormattedResponseXml = String.Empty;

		/// <summary>
		/// Initialises each test.
		/// </summary>
		[ SetUp ]
		public void Init( )
		{
			// Loads the configuration for the formatter.
			StringReader Reader = new StringReader( GetConfigXml( ) );
			m_Config = SoapMessageFormatterConfig.Load( Reader );

			// Initialise the formatter.
			m_Formatter = new SoapMessageFormatter( m_Config.WebServices );
			
			// Initialise the response to be formatted.
			SetResponseXml( );
			
			m_MockMessage = new MockSoapMessage( );
			m_MockMessage.SetMessage( m_ResponseXml );

			// Initialise the expected formatted text.
			SetFormattedResponseXml( );

		}

		/// <summary>
		/// Tests that a base 64/compressed xml string is formatted correctly.
		/// </summary>
		[ Test ]
		public void ResponseXmlFormatting( )
		{
			IMessageFormatter Formatter = ( IMessageFormatter )m_Formatter;

			FormattedText FormattedText = Formatter.FormatResponse( String.Empty,
				m_Uri,
				m_WebServiceName,
				m_WebServiceMethod, 
				m_MockMessage );
			string FormattedResponse = FormattedText.Text;

			Assert.AreEqual( FormattedResponse, m_FormattedResponseXml );
		}

		/// <summary>
		/// Set up the compressed/base 64 encoded soap response that will be 
		/// formatted.
		/// </summary>
		private void SetResponseXml( )
		{
			m_ResponseXml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><soap:Body><TestResponse xmlns=\"http://tempuri.org/\"><TestResult><string>&lt;ticket&gt;&lt;user&gt;matt&lt;/user&gt;&lt;password&gt;secret&lt;/password&gt;&lt;/ticket&gt;</string><string>s2HIYEhlyAHCfAY7hnAgWQRkpzDYMOijyAAA</string></TestResult></TestResponse></soap:Body></soap:Envelope>";
		}

		/// <summary>
		/// Set up the expected formatted response.
		/// </summary>
		private void SetFormattedResponseXml( )
		{
			m_FormattedResponseXml = "TestResponse\r\n\r\nstring: \r\n<ticket>\r\n\t<user>matt</user>\r\n\t<password>secret</password>\r\n</ticket>\r\n\r\nstring: \r\n<hello>World</hello>\r\n\r\n";
		}
		
		/// <summary>
		/// Gets the xml configuration for the Soap message formatter object.
		/// </summary>
		/// <returns>The configuration xml.</returns>
		private string GetConfigXml( )
		{
			string Xml = "<soapmessageformatter.config xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>" +
				"<webservice name='Maths' uri='Maths.asmx'>" +
				"<operation name='Test'>" +
				"	<params>" +
				"		<param type='xml' name='string' display='true' />" +
				"		<param type='string' name='test' display='true' />" +
				"	</params>" +
				"	<returnValue type='xml.array' name='' display='true'>" +
				"		<item type='xml' name='' display='true' />" +
				"		<item type='compressedXml' name='' display='true' />" +
				"	</returnValue>" +
				"</operation>" +
				"<operation name='Add'>" +
				"	<params>" +
				"		<param type='string' name='Value1' display='true' />" +
				"		<param type='string' name='Value2' display='true' />" +
				"	</params>" +
				"	<returnValue type='string.array' name='AddResult' display='true'>" +
				"		<item type='string' name='AddResult' display='true' />" +
				"		<item type='compressedXml' name='' display='true' />" +
				"	</returnValue>" +
				"</operation>" +
				"</webservice>" +
				"</soapmessageformatter.config> ";
			
			return Xml;
		}
	}
}
